package com.alibaba.citrus.ots.b2b.trade.order.functions.utils;

import com.alibaba.citrus.ots.b2b.trade.order.facade.common.SetOrderTag;
import com.alibaba.citrus.ots.common.exception.FunctionException;
import com.alibaba.citrus.ots.common.util.PriceConvertUnit;
import com.epoch.app.otsb2btradecenter.dto.request.OrderLineModifyRequest;
import com.epoch.app.otsb2btradecenter.dto.request.OrderLineRemoveRequest;
import com.epoch.app.otsb2btradecenter.enums.OrderStatus;
import com.epoch.app.otsb2btradecenter.sdo.OrderLineSDO;
import com.epoch.app.otsb2btradecenter.sdo.ScItemSDO;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class OrderLineRemoveConverter {
    public static List<OrderLineSDO> orderLineRemoveConverter(OrderLineRemoveRequest request){
        if (CollectionUtils.isEmpty(request.getIds())) {
            //todo 张苏徽这个地方写的错误码还是不对的，还需要进行修改
            throw new FunctionException("ORDER-A-001-01-16-003");
        }
        List<OrderLineSDO> orderLineSDOS = new ArrayList<>();
        for (String id : request.getIds()) {
            OrderLineSDO orderLineSDO =new OrderLineSDO();
            orderLineSDO.setId(id);
            orderLineSDOS.add(orderLineSDO);
        }
        return orderLineSDOS;
    }

    public static OrderLineSDO orderLineModifyConverter(OrderLineModifyRequest request) {
        if (null != request) {
            OrderLineSDO orderLineSDO = new OrderLineSDO();
            ScItemSDO scItemSDO = ScItemSDOConverter(request);
            orderLineSDO.setId(request.getSubOrderId());
            orderLineSDO.setRefuseReasonCode(request.getRejectReason());
            orderLineSDO.setRemark(request.getRemark());
            scItemSDO.setInventoryUnitCode(request.getInventoryUnit());
            scItemSDO.setSaleUnit(request.getSalesUnit());
            orderLineSDO.setScItemSDO(scItemSDO);

            return orderLineSDO;
        }
        return new OrderLineSDO();
    }

    private static ScItemSDO ScItemSDOConverter(OrderLineModifyRequest request) {
        ScItemSDO scItemSDO = new ScItemSDO();
        if (null != request.getQuantity()) {
            scItemSDO.setQuantity(Integer.parseInt(request.getQuantity()));
        }
        scItemSDO.setUnitPrice(request.getUnitPrice() == null ? null : PriceConvertUnit.convertToCent(request.getUnitPrice()));
        return scItemSDO;
    }

    public static OrderLineSDO converterStatus(OrderLineSDO orderLineSDO) {
        orderLineSDO.setStatus(OrderStatus.COMPLETED.getCode());
        return orderLineSDO;
    }

    public static OrderLineSDO converterStatusOne(OrderLineSDO orderLineSDO) {
        SetOrderTag.setOrderLineSDOStatus(orderLineSDO);
        return orderLineSDO;
    }

    public static OrderLineSDO converterStatusDelivery(OrderLineSDO orderLineSDO) {
        //orderLineSDO.setStatus(OrderStatus.BEFORE_DELIVERY.getCode());
        SetOrderTag.setOrderLineSDOStatusOne(orderLineSDO);
        return orderLineSDO;
    }
}
